﻿using KsMusic.API;
using KsMusic.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace KsMusic.ControlCenter
{
    public class MusicController
    {
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<MPlaySong> QuerySong(string key)
        {
            MQuerySong model = BaiduMusicAPI.QuerySong(key);

            List<MPlaySong> listMusic = new List<MPlaySong>();
            MPlaySong playsong = null;
            foreach (MQuerySongItem item in model.song)
            {
                playsong = new MPlaySong();
                playsong = BaiduMusicAPI.PlaySong(item.songid);
                playsong.songinfo.author = playsong.songinfo.author.Replace(",", "&");

                listMusic.Add(playsong);
            }

            return listMusic;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public MSongList GetSongList(int type, int offset)
        {
            MSongList songlist = BaiduMusicAPI.GetSongList(type, 40, offset);

            return songlist;
        }

        /// <summary>
        /// 缓存音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string DownLoadSong(MPlaySong modPlaySong)
        {
            // 获取播放信息
            // MPlaySong modPlaySong = BaiduMusicAPI.PlaySong(model.songid);

            // 默认存放路径
            string filepath = Environment.CurrentDirectory.Substring(0, 3) + "KsMusic\\Cache";
            // 创建目录
            if (!Directory.Exists(filepath))
            {
                // 创建文件夹并赋予权限
                Directory.CreateDirectory(filepath);   //创建文件夹
                DirectoryInfo dir_info = new DirectoryInfo(filepath);
                DirectorySecurity dir_security = new DirectorySecurity();
                //dir_security.AddAccessRule(new FileSystemAccessRule("Everyone ", FileSystemRights.WriteData, AccessControlType.Allow));
                dir_security.AddAccessRule(new FileSystemAccessRule(System.Environment.UserName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                dir_info.SetAccessControl(dir_security);
            }

            filepath = filepath + "\\" + modPlaySong.songinfo.author + "-" + modPlaySong.songinfo.title + "." + modPlaySong.bitrate.file_extension;

            // 本地检查是否存在
            if (File.Exists(filepath))
            {
                return filepath;
            }

            // 通过播放信息中的文件url去下载文件
            string path = BaiduMusicAPI.DownLoadSong(modPlaySong.bitrate.file_link, filepath, false);

            return path;
        }

        /// <summary>
        /// 缓存音乐
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string DownLoadSong(MSongListItem model)
        {
            // 获取播放信息
            MPlaySong modPlaySong = BaiduMusicAPI.PlaySong(model.song_id);

            // 默认存放路径
            string filepath = Environment.CurrentDirectory.Substring(0, 3) + "KsMusic\\Cache";
            // 创建目录
            if (!Directory.Exists(filepath))
            {
                // 创建文件夹并赋予权限
                Directory.CreateDirectory(filepath);   //创建文件夹
                DirectoryInfo dir_info = new DirectoryInfo(filepath);
                DirectorySecurity dir_security = new DirectorySecurity();
                //dir_security.AddAccessRule(new FileSystemAccessRule("Everyone ", FileSystemRights.WriteData, AccessControlType.Allow));
                dir_security.AddAccessRule(new FileSystemAccessRule(System.Environment.UserName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                dir_info.SetAccessControl(dir_security);
            }

            filepath = filepath + "\\" + modPlaySong.songinfo.author + "-" + modPlaySong.songinfo.title + "." + modPlaySong.bitrate.file_extension;

            // 本地检查是否存在
            if (File.Exists(filepath))
            {
                return filepath;
            }

            // 通过播放信息中的文件url去下载文件
            string path = BaiduMusicAPI.DownLoadSong(modPlaySong.bitrate.file_link, filepath, false);

            return path;
        }
    }
}
